package com.nhc.activity_fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.astrob.activitys.AppContext;
import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;
import com.nhc.model.TripDataFileHandle;
import com.nhc.model.TripDatainfo;
import com.nhc.model.TripItem;
import com.nhc.view.TripItemViewLayout;

import zuo.biao.library.base.BaseFragment;
import zuo.biao.library.manager.TimeRefresher;
import zuo.biao.library.ui.AlertDialog;

/**
 * 使用方法：复制>粘贴>改名>改代码
 */

/**
 * fragment示例
 *
 * @use new DemoFragment(),具体参考.DemoFragmentActivity(initData方法内)
 */
public class TripFragment extends BaseFragment implements View.OnClickListener, TimeRefresher.OnTimeRefreshListener,
        zuo.biao.library.ui.AlertDialog.OnDialogButtonClickListener {
    private final String TAG = "TripFragment";

    //与Activity通信<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 创建一个Fragment实例
     *
     * @return
     */
    public static TripFragment createInstance() {
        return new TripFragment();
    }

    //与Activity通信>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        if (SysUtils.getInstance().is240x320) {
            setContentView(R.layout.trip_fragment_240x320);
        } else {
            setContentView(R.layout.trip_fragment);
        }


        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

        return view;//返回值必须为view
    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    private TripItemViewLayout tripiemview[] = new TripItemViewLayout[8];
    private int[] LL_CTRL = {R.id.tripiemview_0, R.id.tripiemview_1, R.id.tripiemview_2, R.id.tripiemview_3,
            R.id.tripiemview_4, R.id.tripiemview_5, R.id.tripiemview_6, R.id.tripiemview_7};

    @Override
    public void initView() {//必须在onCreateView方法内调用
        for (int i = 0; i < LL_CTRL.length; i++) {
            tripiemview[i] = (TripItemViewLayout) findViewById(LL_CTRL[i]);
        }
    }

    private void clear() {
        TimeRefresher.getInstance().removeTimeRefreshListener(TAG);
        count = 0;
    }


    private boolean isToStop = false;

    private void stopOrContinu() {
        isToStop = !isToStop;
    }
    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    private TripDatainfo tripDatainfo;

    @Override
    public void initData() {//必须在onCreateView方法内调用
        tripDatainfo = TripDataFileHandle.getInstance().mTripDatainfo;
        refreshData();
    }

    private void refreshData() {
        for (int i = 0; i < 8; i++) {
            int index = tripDatainfo.DATATYPE_INDEXS[i];
            if (index < 0 || index >= tripDatainfo.DATATYPE_NAMES.length) {
                continue;
            }
            String name = tripDatainfo.DATATYPE_NAMES[index];
            String CC[] = tripDatainfo.getInfo(index);
            tripiemview[i].bindView(new TripItem(name, CC[0], CC[1]));
        }
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    TextView iv_refresh;

    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须在onCreateView方法内调用
        for (int i = 0; i < tripiemview.length; i++) {
            tripiemview[i].setOnClickListener(this);
        }

        iv_refresh = (TextView) findViewById(R.id.iv_refresh);
        iv_refresh.setTypeface(AppContext.getInstances().getIconfont());
        iv_refresh.setOnClickListener(this);

    }

    @Override
    public void onTimerStart() {

    }

    private int count = 0;

    @Override
    public void onTimerRefresh() {
        if (isToStop == false) {
            count++;
            refreshData();
        }
    }

    @Override
    public void onTimerStop() {

    }

    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void onClick(View v) {//直接调用不会显示v被点击效果
        for (int i = 0; i < LL_CTRL.length; i++) {
            if (v.getId() == LL_CTRL[i]) {
                toActivity(ChangeDataTypeActivity.createIntent(getContext(), tripDatainfo.DATATYPE_INDEXS[i]), i);
                break;
            }
        }

        if (v.getId() == R.id.iv_refresh) {
            onPopMenu();
        }

    }

    public void onPopMenu() {
        new AlertDialog(context, getString(R.string.alert_title), getString(R.string.alert_resetrinfo), true, 0, this).show();
    }

    boolean _isFirst = true;

    public void setFoucseCtrl() {
        if (!_isFirst || findViewById(LL_CTRL[0]) == null) {
            return;
        }

        _isFirst = false;

        for (int i = 0; i < LL_CTRL.length; i++) {
            findViewById(LL_CTRL[i]).setFocusable(true);
        }

        findViewById(LL_CTRL[0]).requestFocus();
    }

    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    @Override
    public void onResume() {
        super.onResume();
        TimeRefresher.getInstance().addTimeRefreshListener(TAG, 1000, this);
    }

    @Override
    public void onPause() {
        super.onPause();
        TimeRefresher.getInstance().removeTimeRefreshListener(TAG);
    }

    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (RESULT_OK == resultCode) {
            refreshData();
        } else {
            if (requestCode >= 0 && requestCode < 8) {
                tripDatainfo.DATATYPE_INDEXS[requestCode] = resultCode;
                TripDataFileHandle.getInstance().save();
                refreshData();
            }
        }
    }

    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {
        if (isPositive == false) {
            return;
        }

        if (requestCode == 0) {
            TripDataFileHandle.getInstance().mTripDatainfo.reset();
            TripDataFileHandle.getInstance().save();
            refreshData();
        }
    }
    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


}